package com.example.venue_reservation_service.controller;


import com.example.venue_reservation_service.entity.ChatRequest;
import com.example.venue_reservation_service.service.ChatService;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;


@RestController
@RequestMapping("/chat")
@CrossOrigin
public class ChatController {

    @Resource
    private ChatService chatService;

    @PostMapping(value = "/ask", produces = MediaType.TEXT_PLAIN_VALUE)
    public void chat(@RequestBody ChatRequest request, HttpServletRequest httpRequest, HttpServletResponse response) throws IOException {

        // 设置流式响应头
        response.setContentType("text/event-stream");
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setHeader("Cache-Control", "no-cache");

        // 获取输出流
        ServletOutputStream outputStream = response.getOutputStream();

        // 处理流式响应
        chatService.streamChatResponse(request, outputStream, httpRequest);
    }
}